<?php
class Admin_SlidersController extends Zend_Controller_Action {
	public function init() {
		$layoutPath = APPLICATION_PATH . '/templates/admin';
		$options = array (
				'layout' => 'index',
				'layoutPath' => $layoutPath 
		);
		Zend_Layout::startMvc ( $options );
	}
	public function indexAction() {
		$Condition = array ();
		list ( $this->view->Pager, $this->view->Sliders ) = Sliders::getAll ( $Condition );
	}
	public function createAction() {
		$requests = $this->getRequest ()->getParams ();
		if ($this->getRequest ()->isPost ()) {
			$uploadDir = PUBLIC_PATH . "/uploads/sliders/";
			try {
				$Slider = new Sliders ();
				$Slider->merge ( $requests );
				$Slider->created_date = date ( 'Y-m-d H:i:s', time () );
				$User = new Zend_Session_Namespace ( 'Login' );
				$Slider->created_by = $User->User->id;
				if ($_FILES) {
					$image = self::_uploadFiles ( $uploadDir, Lib_General::text2url ( $requests ['title'] ) );
					$Slider->image = Lib_General::text2url ( $requests ['title'] ) . '.jpg';
				}
				$Slider->save ();
				$this->view->Message = 'Create slider successfull';
			} catch ( Exception $e ) {
				self::_deleteFile ( $uploadDir . Lib_General::text2url ( $requests ['title'] ) . '.jpg' );
				print_r ( $e->getMessage () );
				die ();
			}
		} else {
			unset ( $requests ['id'] );
		}
	}
	private static function _uploadFiles($uploadDir = '', $FileName = '') {
		// print_r($uploadDir);die();
		if (! is_dir ( $uploadDir )) {
			@mkdir ( $uploadDir );
			chmod ( $uploadDir, 0777 );
		}
		// upload thumb image
		$image = new Zend_Form_Element_File ( 'image' );
		$image->setDestination ( $uploadDir );
		$image->addValidator ( 'Count', false, array (
				'min' => 1,
				'max' => 1 
		) );
		// $ExtendFile = pathinfo($FileName);
		// $FileName= basename($FileName,'.'.$ExtendFile['extension']);
		$image->addValidator ( 'Extension', false, 'jpg,png,gif' );
		$image->addFilter ( 'Rename', array (
				'target' => $uploadDir . $FileName . '.jpg',
				'overwrite' => true 
		) );
		$image->receive ();
		return $image->getFileName ( null, false );
	}
	public static function _deleteFile($FileName = '') {
		if (file_exists ( $FileName )) {
			unlink ( $FileName );
		}
	}
	public function editAction() {
		$requests = $this->getRequest ()->getParams ();
		
		$Slider = Sliders::getById ( $requests ['id'] );
		
		if ($Slider) {
			
			$this->view->Slider = $Slider;
			if ($this->getRequest ()->isPost ()) {
				try {
					$Slider->merge ( $requests );
					
					$Slider->modified_date = date ( 'Y-m-d H:i:s', time () );
					$User = new Zend_Session_Namespace ( 'Login' );
					$Slider->modified_by = $User->User->id;
					$uploadDir = PUBLIC_PATH . "/uploads/sliders/";
					if ($_FILES && $_FILES ['image'] ['name'] != '') {
						
						self::_deleteFile ( $uploadDir . $Slider->image );
						$image = self::_uploadFiles ( $uploadDir, Lib_General::text2url ( $requests ['title'] ) );
						$Slider->image = Lib_General::text2url ( $requests ['title'] ) . '.jpg';
					}
					$Slider->save ();
					$this->view->Message = 'Update slider successfull';
				} catch ( Exception $e ) {
					if ($_FILES) {
						$image = self::_uploadFiles ( $uploadDir, $Categories->title );
						$Slider->image = Lib_General::text2url ( $Categories->title ) . '.jpg';
						$Slider->save ();
					}
					print_r ( $e->getMessage () );
					die ();
				}
			}
		} else {
			$this->_redirect ( '/admin/sliders' );
		}
	}
	public function deleteAction() {
		$this->_helper->layout->disableLayout ();
		$this->_helper->viewRenderer->setNoRender ( true );
		$json = array (
				'STATUS' => 0 
		);
		try {
			$requests = $this->getRequest ()->getParams ();
			$Slider = Sliders::getById ( $requests ['id'] );
			$thumbnail = $Slider->image;
			$Slider->delete ();
			self::_deleteFile ( PUBLIC_PATH . '/uploads/sliders/' . $thumbnail );
			$json ['STATUS'] = 1;
		} catch ( Exception $e ) {
			$json ['STATUS'] = 0;
		}
		header ( 'Content-type: application/json' );
		echo json_encode ( $json );
	}
	public function deletemultiAction() {
		$this->_helper->layout->disableLayout ();
		$this->_helper->viewRenderer->setNoRender ( true );
		
		try {
			$requests = $this->getRequest ()->getParams ();
			
			$arrID = $requests ['sliders'];
			foreach ( $arrID as $key => $value ) {
				
				$Slider = Sliders::getById ( $value );
				
				if ($Slider) {
					$thumbnail = $Slider->image;
					$Slider->delete ();
					self::_deleteFile ( PUBLIC_PATH . '/uploads/sliders/' . $thumbnail );
				}
			}
			$this->_redirect ( '/admin/sliders/index' );
		} catch ( Exception $e ) {
			print_r ( $e->getMessage () );
			die ();
		}
	}
}







